package com.heima.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.pojo.Policy;
import com.heima.pojo.VmPolicy;
import com.heima.pojo.dto.PolicyDto;
import com.heima.pojo.dto.PolicyPutDto;
import com.heima.pojo.vo.VmListVo;
import com.heima.service.IPolicyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/vm-service/policy")
@RequiredArgsConstructor
public class PolicyController {

    private final IPolicyService policyService;

    /**
     * 新增策略
     */
    @PostMapping
    public Boolean savePolicy(@RequestBody PolicyPutDto policyDto) {
        Boolean isTrue = policyService.savePolicy(policyDto);
        return isTrue;
    }

    /**
     * 分页查询策略
     * @param policyDto
     * @return
     */
    @GetMapping("/search")
    private PageResult<Policy> search(PolicyDto policyDto) {
        PageResult<Policy> pageResult = policyService.search(policyDto);
        return pageResult;
    }

    /**
     * 修改策略
     * @param policyId
     * @param policyPutDto
     * @return
     */
    @PutMapping("/{policyId}")
    private Boolean updatePolicy(@PathVariable Integer policyId, @RequestBody PolicyPutDto policyPutDto) {
        return policyService.updatePolicy(policyId, policyPutDto);
    }

    /**
     * 删除策略
     * @param policyId
     * @return
     */
    @DeleteMapping("/{policyId}")
    private Boolean deletePolicy(@PathVariable Integer policyId) {
        //做判断，如果有机器绑定了该策略则不能删除
        return policyService.removePolicy(policyId);
    }

    /**
     * 获取机器策略
     * @param innerCode
     * @return
     */
    @GetMapping("/vmPolicy/{innerCode}")
    private VmPolicy getVmPolicy(@PathVariable String innerCode) {
        VmPolicy vmPolicy = policyService.getVmPolicy(innerCode);
        return vmPolicy;
    }

    /**
     * 查看机器策略
     * @param policyId
     * @param policyDto
     * @return
     */
    @GetMapping("/vmList/{policyId}")
    private PageResult<VmListVo> getVmList(@PathVariable Integer policyId, PolicyDto policyDto) {
        PageResult<VmListVo> vmList = policyService.getVmList(policyId,policyDto);
        return vmList;
    }
}

